﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using System.Text;
using System.IO;
using System.Data;
using System.Collections;

namespace webs.website_admin.integration
{
    public partial class integralHistoryList : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Expires = -1;
            PageBegin();

            switch (RequestBase.GetFormString("otype"))
            {
                case "loadingdata":
                    LoadingData();
                    break;
                case "deleteitems":
                    Delete();
                    break;
            }
        }

        #region 信息绑定
        void LoadingData()
        {
            string ctrl = "~/website_admin/integration/_controls/ctlIntegralHistoryList.ascx";

            string SQL = " and DeletionStateCode = 0 ";

            if (!string.IsNullOrEmpty(RequestBase.GetFormString("username")))
            {
                SQL += " AND UserName LIKE '%" + RequestBase.GetFormString("username") + "%'";
            }          

            Control uc_rep = LoadControl(ctrl);

            Repeater rptProductList = (Repeater)uc_rep.FindControl("rptIntegralMagList");

            DBUtility.Pagination pagination = new DBUtility.Pagination();
            try
            {
                if (!string.IsNullOrEmpty(RequestBase.GetString("index")))
                    pagination.Sequence = Convert.ToInt32(RequestBase.GetString("index"));
                else
                    pagination.Sequence = 1;

                pagination.ItemUnit = "条记录";
                pagination.SortSentence = " ORDER BY ID ASC";
                pagination.Primarykey = "Id";
                pagination.SearchField = "*";
                pagination.TableName = "vw_IntegrationHistory";
                if (!string.IsNullOrEmpty(RequestBase.GetString("pagesize")))
                    pagination.PageSize = Convert.ToInt32(RequestBase.GetString("pagesize"));
                else
                    pagination.PageSize = 20;
                pagination.UrlStr = "";
                pagination.ImagesPath = "../_imgs/grid/";
                pagination.SearchSentence = SQL.Trim();

                rptProductList.DataSource = pagination.GetDataTable();
                rptProductList.DataBind();

                pagination.PageBarFlexigrid();
                uc_rep.GetType().GetProperty("PaginationBar").SetValue(uc_rep, pagination.PageBar, null);

                StringBuilder strb = new StringBuilder();
                StringWriter sw = new StringWriter(strb);
                HtmlTextWriter htw = new HtmlTextWriter(sw);

                rptProductList.RenderControl(htw);

                BLL.AjaxHelper.ResponseWrite(strb.ToString());
            }
            catch (System.Threading.ThreadAbortException err)
            {
                //nothing   to   do   
            }
            catch (Exception err)
            {
                BLL.AjaxHelper.ResponseWrite(err.Message);
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除目录
        /// </summary>
        void Delete()
        {
            string itemsid = RequestBase.GetFormString("itemsid").Replace("'", "").Replace("'", "");

            string res = "";
            foreach (string objectid in itemsid.Split(','))
            {
                /// 逻辑删除商品记录
                res = BLL.IntegralBase.Delete(
                    objectid,
                    true
                    );
            }

            BLL.AjaxHelper.ResponseWrite(res);
        }
        #endregion
    }
}